package com.xiyuan;

import com.xiyuan.bean.Result;
import com.xiyuan.exception.BuildPathException;
import com.xiyuan.util.ResourceUtil;
import lombok.Data;

import java.util.Map;

/**
 * 路径创建指挥者
 */
@Data
public class PathDirector {
    private AbstractPathBuilder builder;
    private Class<?> clazz;

    private static Map<String, Object> yml;

    static {
        yml = ResourceUtil.getYml(PathDirector.class);
    }

    public PathDirector builder(AbstractPathBuilder builder, Class<?> clazz) {
        this.builder = builder;
        this.clazz = clazz;
        return this;
    }

    public PathDirector builder(Class<?> clazz) throws Exception {
        if (yml == null || yml.size() == 0) {
            throw new Exception("yml初始化失败，注意resources目录下面是否有interface.yml");
        }
        Map<String, String> o = (Map<String, String>) yml.get("build");
        if (o == null || o.size() == 0) {
            throw new Exception("build属性为空");
        }
        String type = o.get("type");
        if (type.equals("swagger"))
            this.builder = new Swagger2PathBuilder();
        else
            throw new BuildPathException("没有对应类型的构建器");
        this.clazz = clazz;
        return this;
    }

    /**
     * 建造出builder对象
     */
    public Result build() {
        //获取模块命名
        builder.getTags(this.clazz);
        //获取总路由中的注解
        builder.getRoutePath(this.clazz);
//        获取方法上的注解
        builder.getMethodAnnotations(this.clazz);
        //解析方法上的注解
        builder.parseMethodAnnotations();
        //将路径命名
        builder.namePath();
        //生成json文件
        builder.createJson();
        //返回builder中的result
        return builder.getResult();
    }
}
